using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace IceAge.Administration
{
    public partial class CompStructTree : DockContent
    {
        public CompStructTree()
        {
            InitializeComponent();
        }
        private DataSet comp = null;
        private void AddTreeNode(ref TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["PARENT_ID"]) == Convert.ToInt32(parent.Tag))
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["title"].ToString();
                    child.Tag = row["node_id"];
                    parent.Nodes.Add(child);
                    child.StateImageIndex = 1;
                    AddTreeNode(ref child, menus);
                }
            }
        }

        private void CompStructTree_Load(object sender, EventArgs e)
        {
            try
            {
                comp = CommonLibrary.GetDataSetFromProcedure("SELECT * FROM CompStructTree", string.Empty);
                if (comp != null)
                {
                    foreach (DataRow row in comp.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                        {
                            TreeNode root = new TreeNode();
                            root.Text = row["title"].ToString();
                            root.Tag = row["node_id"];
                            root.StateImageIndex = 0;
                            treeView1.Nodes.Add(root);
                            AddTreeNode(ref root, comp);
                        }
                    }
                }
                //menu_count = menus.Tables[0].Rows.Count;
                treeView1.ExpandAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }
    }
}